﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IP.Core.IPData;
using IP.Core.IPUserService;
using BKIDS;
using BKIDS.CDBNames;
using BKIUS;
using IP.Core.IPCommon;

public partial class DanhMuc_F601_Cong_viec_du_an : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_data_2_combo_loai_da_s();
            load_data_2_combo_loai_da();
            load_data_2_combo_don_vi_tinh();
        }
    }
    #region Members
    US_DM_CONG_VIEC_DU_AN m_us_cong_viec_du_an = new US_DM_CONG_VIEC_DU_AN();
    DS_DM_CONG_VIEC_DU_AN m_ds_cong_viec_du_an = new DS_DM_CONG_VIEC_DU_AN();
    #endregion

    #region Public Interfaces
    public string mapping_loai_du_an_by_id(object ip_obj_id_loai_du_an)
    {
        US_CM_DM_TU_DIEN v_us_tu_dien = new US_CM_DM_TU_DIEN(CIPConvert.ToDecimal(ip_obj_id_loai_du_an));
        return v_us_tu_dien.strTEN;
    }
    public string mapping_don_vi_tinh_by_id(object ip_obj_id_don_vi_tinh)
    {
        US_CM_DM_TU_DIEN v_us_tu_dien = new US_CM_DM_TU_DIEN(CIPConvert.ToDecimal(ip_obj_id_don_vi_tinh));
        return v_us_tu_dien.strTEN;
    }
    #endregion

    #region Private Methods
    private void load_data_2_combo_loai_da_s()
    {
        US_CM_DM_TU_DIEN v_us_tu_dien = new US_CM_DM_TU_DIEN();
        DS_CM_DM_TU_DIEN v_ds_tu_dien = new DS_CM_DM_TU_DIEN();

        v_us_tu_dien.FillDataset(v_ds_tu_dien," WHERE ID_LOAI_TU_DIEN = 5");

        m_cbo_loai_du_an_s.DataTextField = CM_DM_TU_DIEN.TEN;
        m_cbo_loai_du_an_s.DataValueField = CM_DM_TU_DIEN.ID;

        m_cbo_loai_du_an_s.DataSource = v_ds_tu_dien.CM_DM_TU_DIEN;
        m_cbo_loai_du_an_s.DataBind();
    }
    private void load_data_2_combo_loai_da()
    {
        US_CM_DM_TU_DIEN v_us_tu_dien = new US_CM_DM_TU_DIEN();
        DS_CM_DM_TU_DIEN v_ds_tu_dien = new DS_CM_DM_TU_DIEN();

        v_us_tu_dien.FillDataset(v_ds_tu_dien, " WHERE ID_LOAI_TU_DIEN = 5");

        m_cbo_loai_du_an.DataTextField = CM_DM_TU_DIEN.TEN;
        m_cbo_loai_du_an.DataValueField = CM_DM_TU_DIEN.ID;

        m_cbo_loai_du_an.DataSource = v_ds_tu_dien.CM_DM_TU_DIEN;
        m_cbo_loai_du_an.DataBind();
    }
    private void load_data_2_combo_don_vi_tinh()
    {
        US_CM_DM_TU_DIEN v_us_tu_dien = new US_CM_DM_TU_DIEN();
        DS_CM_DM_TU_DIEN v_ds_tu_dien = new DS_CM_DM_TU_DIEN();

        v_us_tu_dien.FillDataset(v_ds_tu_dien, " WHERE ID_LOAI_TU_DIEN = 9");

        m_cbo_don_vi_tinh.DataTextField = CM_DM_TU_DIEN.TEN;
        m_cbo_don_vi_tinh.DataValueField = CM_DM_TU_DIEN.ID;

        m_cbo_don_vi_tinh.DataSource = v_ds_tu_dien.CM_DM_TU_DIEN;
        m_cbo_don_vi_tinh.DataBind();
    }
    private void load_data_2_grv_by_loai_du_an()
    {
        m_us_cong_viec_du_an.FillDataset(m_ds_cong_viec_du_an, " WHERE ID_LOAI_DU_AN=" + CIPConvert.ToDecimal(m_cbo_loai_du_an_s.SelectedValue));
        m_grv_cong_viec_du_an.DataSource = m_ds_cong_viec_du_an.DM_CONG_VIEC_DU_AN;
        m_grv_cong_viec_du_an.DataBind();
    }
    private void load_data_2_grv()
    {
        m_us_cong_viec_du_an.FillDataset(m_ds_cong_viec_du_an);
        m_grv_cong_viec_du_an.DataSource = m_ds_cong_viec_du_an.DM_CONG_VIEC_DU_AN;
        m_grv_cong_viec_du_an.DataBind();
    }
    private bool check_trung_cv_da(string ip_str_ten_du_an, decimal ip_dc_loai_du_an, out string op_str_cv_da_trung)
    {
        m_us_cong_viec_du_an.check_trung_cv_da(m_ds_cong_viec_du_an, ip_str_ten_du_an, ip_dc_loai_du_an);
        op_str_cv_da_trung = "";
        if (m_ds_cong_viec_du_an.DM_CONG_VIEC_DU_AN.Rows.Count > 0)
        {
            op_str_cv_da_trung = "Công việc của loại dự án đã tồn tại ";
            return false;
        }
        return true;
    }
    private void form_2_us_object()
    {
        m_us_cong_viec_du_an.strTEN_CONG_VIEC = m_txt_ten_cong_viec.Text;
        m_us_cong_viec_du_an.dcID_LOAI_DU_AN = CIPConvert.ToDecimal(m_cbo_loai_du_an.SelectedValue);
        m_us_cong_viec_du_an.strMA_DON_VI_TINH = CIPConvert.ToStr(m_cbo_don_vi_tinh.SelectedValue);
        if (m_txt_hs_default.Text != "")
        {
            m_us_cong_viec_du_an.dcHE_SO_DEFAULT = CIPConvert.ToDecimal(m_txt_hs_default.Text);
        }
        else
        {
            m_us_cong_viec_du_an.dcHE_SO_DEFAULT = 0;
        }
        m_us_cong_viec_du_an.strGHI_CHU = m_txt_ghi_chu.Text;
    }

    private void load_data_2_us_update(int ip_i_stt_row)
    {
        decimal v_dc_id_cong_viec_du_an = CIPConvert.ToDecimal(m_grv_cong_viec_du_an.DataKeys[ip_i_stt_row].Value);
        m_txt_ghi_chu.ToolTip = CIPConvert.ToStr(v_dc_id_cong_viec_du_an);
        m_us_cong_viec_du_an = new US_DM_CONG_VIEC_DU_AN(v_dc_id_cong_viec_du_an);
    }
    private void us_object_2_form(US_DM_CONG_VIEC_DU_AN ip_us_dm_cong_viec_du_an)
    {
        m_txt_ten_cong_viec.Text = ip_us_dm_cong_viec_du_an.strTEN_CONG_VIEC;
        m_cbo_loai_du_an.SelectedValue = CIPConvert.ToStr(ip_us_dm_cong_viec_du_an.dcID_LOAI_DU_AN);
        m_cbo_don_vi_tinh.SelectedValue = CIPConvert.ToStr(ip_us_dm_cong_viec_du_an.strMA_DON_VI_TINH);
        m_txt_hs_default.Text = CIPConvert.ToStr(ip_us_dm_cong_viec_du_an.dcHE_SO_DEFAULT);
        m_txt_ghi_chu.Text = ip_us_dm_cong_viec_du_an.strGHI_CHU;
    }

    private void xoa_trang_form()
    {
        m_txt_ten_cong_viec.Text = "";
        m_txt_hs_default.Text = "";
        m_txt_ghi_chu.Text = "";
    }
    private void search_du_an_and_load_2_grv()
    {
        decimal v_dc_id_loai_du_an = CIPConvert.ToDecimal(m_cbo_loai_du_an_s.SelectedValue);
        m_us_cong_viec_du_an.loc_du_lieu_du_an(m_ds_cong_viec_du_an, v_dc_id_loai_du_an);
        m_grv_cong_viec_du_an.DataSource = m_ds_cong_viec_du_an.DM_CONG_VIEC_DU_AN;
        m_grv_cong_viec_du_an.DataBind();
        m_lbl_nhan.Text = "Kết quả lọc dữ liệu : " + m_ds_cong_viec_du_an.DM_CONG_VIEC_DU_AN.Count + " bản ghi phù hợp .";
        if (m_ds_cong_viec_du_an.DM_CONG_VIEC_DU_AN.Count == 0)
        {
            m_lbl_nhan.Text = "Kết quả lọc dữ liệu";
            m_lbl_thong_bao.Text = "Không có bản ghi nào phù hợp.";
        }
        load_data_2_grv_by_loai_du_an();
    }
    #endregion
    protected void m_cbo_loai_du_an_s_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            load_data_2_grv_by_loai_du_an();
        }
        catch (Exception v_e)
        {
            this.Response.Write(v_e.ToString());
        }
    }
    protected void m_cmd_xem_toan_bo_Click(object sender, EventArgs e)
    {
        m_lbl_nhan.Text = "Kết quả lọc dữ liệu";
        load_data_2_grv();
        m_lbl_thong_bao.Visible = false;
    }
    protected void m_cmd_them_moi_Click(object sender, EventArgs e)
    {
        m_lbl_thong_bao.Visible = true;
        string v_str_result = "";
        if(!check_trung_cv_da(m_txt_ten_cong_viec.Text,CIPConvert.ToDecimal(m_cbo_loai_du_an.SelectedValue),out v_str_result))
        {
            string v_str_script = string.Format("<script language='javascript'>alert('{0}');</script>", v_str_result);
            Page.ClientScript.RegisterStartupScript(this.GetType(),"Oncheck",v_str_script);
            m_txt_ten_cong_viec.Focus();
            m_txt_ten_cong_viec.Text ="";
            return;
        }
        if (m_txt_ten_cong_viec.Text == "")
        {
            m_lbl_thong_bao.Text = "Thông báo : Tên công việc không được bỏ trống!";
            m_txt_ten_cong_viec.Focus();
            m_txt_ten_cong_viec.Text = "";
            return;
        }
            form_2_us_object();
            m_us_cong_viec_du_an.Insert();
            m_lbl_thong_bao.Text = "Thông báo : Thêm thành công !";
            xoa_trang_form();
            load_data_2_grv();
            m_lbl_nhan.Text = "Kết quả lọc dữ liệu";
    }
    protected void m_cmd_xoa_trang_Click(object sender, EventArgs e)
    {
            xoa_trang_form();
            m_lbl_thong_bao.Visible = false;
    }
    protected void m_cmd_loc_du_lieu_Click(object sender, EventArgs e)
    {
        search_du_an_and_load_2_grv();
        m_lbl_thong_bao.Visible = false;
    }
    protected void m_cmd_cap_nhat_Click(object sender, EventArgs e)
    {
        form_2_us_object();
        m_us_cong_viec_du_an.dcID = CIPConvert.ToDecimal(m_txt_ghi_chu.ToolTip);
        m_us_cong_viec_du_an.Update();
        m_lbl_thong_bao.Visible = true;
        m_lbl_thong_bao.Text = " * Cập nhật thành công !";
        load_data_2_grv();
        m_txt_ghi_chu.ToolTip = "";
        xoa_trang_form();
        m_cmd_cap_nhat.Visible = false;
        m_cmd_them_moi.Visible = true;
    }
    protected void m_grv_cong_viec_du_an_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        m_lbl_thong_bao.Visible = false;
        load_data_2_us_update(e.RowIndex);
        us_object_2_form(m_us_cong_viec_du_an);
        m_cmd_them_moi.Visible = false;
        m_cmd_cap_nhat.Visible = true;
    }
    protected void m_grv_cong_viec_du_an_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        decimal v_dc_id_cong_viec_du_an = CIPConvert.ToDecimal(m_grv_cong_viec_du_an.DataKeys[e.RowIndex].Value);
        m_us_cong_viec_du_an.DeleteByID(v_dc_id_cong_viec_du_an);
        m_lbl_thong_bao.Text = " * Xóa bản ghi thành công !";
        load_data_2_grv();
        m_txt_ghi_chu.ToolTip = "";
    }
}